package mobmap.util.google;

import net.dclausen.microfloat.MicroDouble;

public class GoogleMapUtil {

	public static final int offset = GoogleMaps.offset; 
    public static final double radius = GoogleMaps.radius; 

	
	public double[] adjust(double lat, double lng, int deltaX, int deltaY, int z)
	{
		return new double[]{
			XToL(LToX(lng) + (deltaX<<(21-z))),
			YToL(LToY(lat) + (deltaY<<(21-z)))
		};
	}
	double LToX(double x)
	{
		return round(offset + radius * x * Math.PI / 180);
	}
	 
	double LToY(double y)
	{
		return round(
			offset - radius * 
			Double.longBitsToDouble(MicroDouble.log(
				Double.doubleToLongBits(
				(1 + Math.sin(y * Math.PI / 180))
				/
				(1 - Math.sin(y * Math.PI / 180))
				)
			)) / 2);
	}
	 
	double XToL(double x)
	{
		return ((round(x) - offset) / radius) * 180 / Math.PI;
	}
	 
	double YToL(double y)
	{
		return (Math.PI / 2 - 2 * Double.longBitsToDouble(
					MicroDouble.atan(
						MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
					)
				)) * 180 / Math.PI;
	}
	double round(double num)
	{
		double floor = Math.floor(num);
		
		if(num - floor >= 0.5)
			return Math.ceil(num);
		else
			return floor;
	}
}
